﻿using System;
using System.Runtime.InteropServices;
using System.Text;
using pinlib.structs;


namespace pinlib.functions
{
    internal static class User32Callbacks
    {
        /// <summary>
        ///   An application-defined callback function used with the EnumWindows or EnumDesktopWindows function.
        ///   It receives top-level window handles
        /// </summary>
        /// <param name = "windowHandle">A handle to a top-level window.</param>
        /// <param name = "lParam">nothing in this case</param>
        /// <returns>To continue enumeration, the callback function must return TRUE;
        ///   to stop enumeration, it must return FALSE</returns>
        internal static bool EnumWindowsProc(IntPtr windowHandle, int lParam)
        {
            StringBuilder winText = new StringBuilder(256);
            if (!User32Native.IsWindowVisible(windowHandle))
            {
                return true;
            }
            if (User32Native.GetWindowText(windowHandle, winText, winText.Capacity) == 0)
            {
                return true;
            }

            if (winText.Length > 0)
            {
                WindowList.Add(windowHandle, winText.ToString());
            }
            return true;
        }

        /// <summary>
        ///   Used with the EnumChildWindows function. It receives the child window handles.
        /// </summary>
        /// <param name = "windowHandle">A handle to a child window of the parent window specified in EnumChildWindows</param>
        /// <param name = "lParam">The application-defined value given in EnumChildWindows</param>
        /// <returns>o continue enumeration, the callback function must return TRUE; to stop enumeration, it must return FALSE</returns>
        internal static bool EnumChildProc(IntPtr windowHandle, int lParam)
        {
            StringBuilder winText = new StringBuilder(256);

            if (User32Native.GetWindowText(windowHandle, winText, winText.Capacity) == 0)
            {
                return true;
            }
            if (winText.Length > 0)
            {
                ChildList.Add(windowHandle, winText.ToString());
                return false;
            }
            return true;
        }
    }
}